<template>
  <div>
    <el-dialog
      :title="$t('email.EditSendTime')"
      :close-on-click-modal="false"
      width="540px"
      :visible.sync="this.dialogVisible"
      append-to-body
      :before-close="handleClose"
    >
      <el-form
        label-width="150px"
        :rules="rules"
        :model="ruleForm"
        ref="ruleForm"
      >
        <el-form-item :label="$t('email.originSendTime')">
          <el-date-picker
            v-model="ruleForm.timeOld"
            type="datetime"
            disabled
            default-time="12:00:00"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('email.newSendTime')" prop="timeNew">
          <el-date-picker
            v-model="ruleForm.timeNew"
            type="datetime"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button
          type="primary"
          @click="submitEditTimingTransmission('ruleForm')"
          >{{ $t('permission.confirm') }}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { postAction } from '@/api/common'
import { parseTime } from '@/utils'
export default {
  name: 'AddGroup',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    timingTransmission: {
      type: String,
      default: '2021-01-01 00:00:00',
    },
    id: {
      Type: Number,
      default: 0,
    },
  },
  components: {},
  watch: {
    show() {
      this.dialogVisible = this.show
    },
    id() {
      this.ruleForm.id = this.id
    },
    timingTransmission() {
      this.ruleForm.timeOld = this.timingTransmission
    },
  },
  data: function () {
    return {
      timeNow: parseTime(new Date()),
      ruleForm: {
        timeOld: '',
        timeNew: parseTime(new Date()),
        id: 0,
      },
      groupId: null,
      tableData: [],
      options: [],
      dialogVisible: this.show,
      rules: {
        timeNew: [
          {
            required: true,
            message: this.$t('common.select', [this.$t('email.newSendTime')]),
            trigger: 'blur',
          },
        ],
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now()
        },
      },
    }
  },
  methods: {
    handleClick(row) {
      console.log(row)
    },
    getInit() {
      // getAction({}, 'EmailConfigGroup/GetGroupList')
      //   .then((res) => {
      //     this.options = res.data
      //   })
    },
    handleClose() {
      this.resetForm('ruleForm')
      this.$emit('update:show', false)
    },
    submitEditTimingTransmission(formName) {
      if (Date.parse(this.ruleForm.timeNew) - Date.now() < 5 * 60) {
        this.$confirm(
          this.$t('email.confirmSendEmailImmediately'),
          this.$t('common.tips'),
          {
            confirmButtonText: this.$t('common.confirm'),
            cancelButtonText: this.$t('common.cancel'),
            type: 'warning',
          }
        ).then(() => {
          this.doSubmit(formName)
        })
      } else {
        this.doSubmit(formName)
      }
    },
    doSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          postAction(
            this.ruleForm,
            'EmailMarketing/EditTimingTransmission'
          ).then((res) => {
            if (res.code === 20000) {
              this.$message({
                message: this.$t('email.editSendTimeSuccessfully'),
                type: 'success',
              })
              this.handleClose(false)
              this.$emit('closeAction')
            } else {
              this.$message({
                message: res.msg,
                type: 'warning',
              })
            }
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
  },
}
</script>

<style scoped></style>
